package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-12-06
 * Time：11:30
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        //判断账号或密码是否为空
        if (req.getParameter("username") == null ||
                req.getParameter("password") == null ||
                req.getParameter("username").isEmpty() ||
                req.getParameter("password").isEmpty()) {
            resp.getWriter().write("用户名或密码为空！请重新输入");
            return;
        }
        //判断账号密码是否正确
        if (!(req.getParameter("username").equals("1445469577") &&
                req.getParameter("password").equals("123456"))) {
            resp.getWriter().write("用户名或密码错误！请重新登陆");
            return;
        }
        HttpSession session = req.getSession(true);
        if (session.isNew()) {
            session.setAttribute("username", req.getParameter("username"));
            session.setAttribute("visitCount", 0);
        }
        resp.sendRedirect("index");
    }
}
